package com.deco.back.tags;

import com.deco.domain.vo.sys.CmsMenuVo;
import com.deco.domain.vo.sys.CmsRoleVo;
import com.deco.domain.vo.sys.CmsUserVo;
import com.deco.service.sys.CmsMenuService;
import com.deco.service.sys.CmsRoleService;
import com.free.ext.shiro.SessionManager;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Component
public class MenusDirective implements TemplateDirectiveModel {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    @Autowired
    private CmsMenuService menuService;

    @Autowired
    private HttpServletRequest request;
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        String pathInfo = request.getRequestURI();
        CmsUserVo userVo = (CmsUserVo)SecurityUtils.getSubject().getPrincipal();
        List<CmsMenuVo> userMenus = menuService.getMenuTreeByRole(userVo.getRoles(),pathInfo);
        environment.setVariable("menuList", getBeansWrapper().wrap(userMenus));
        templateDirectiveBody.render(environment.getOut());
    }

    public static BeansWrapper getBeansWrapper(){
        BeansWrapper beansWrapper =
                new BeansWrapperBuilder(Configuration.VERSION_2_3_27).build();
        return beansWrapper;
    }
    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
